#include "timer.h"
#include "lvgl.h"

void tim6Init(u16 psc,u16 arr)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; 
    NVIC_InitTypeDef NVIC_InitStruct;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);   

    TIM_TimeBaseInitStructure.TIM_Period = arr-1; //设置自动重装载寄存器周期的值
    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;//设置用来作为TIMx时钟频率预分频值
    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
    TIM_TimeBaseInit(TIM6, & TIM_TimeBaseInitStructure);
    TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE );
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitStruct.NVIC_IRQChannel=TIM6_IRQn; 
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;  
    NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;      
    NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    
    TIM_Cmd(TIM6,ENABLE);//使能定时器
}

void tim3Init(u16 psc,u16 arr)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //①时钟 TIM3 使能
    //定时器 TIM3 初始化
    TIM_TimeBaseStructure.TIM_Period = arr-1; //设置自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler =psc - 1; //设置时钟频率除数的预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );//更新中断使能

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
    TIM_Cmd(TIM3, ENABLE);
}


void TIM3_IRQHandler(void) //处理按键声音
{   

    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查 TIM3 更新中断发生与否
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除 TIM3 更新中断标志
		lv_tick_inc(1);


    }
}


